package org.tea.springdata.mina;

import java.util.Collection;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 数据校验工具类, 暂时只想到这几个需要验证, 如有需要请补充在下面
 * @author John zhang
 * @version 0.1
 */
public class ValidatorUtils {

	private static final String MOBILE_REGEX = "^(13|14|15|18)\\d{9}$";
	private static final String ANY_CHAR_EXPRESSION= "[a-z0-9!#$%&'*+/=?^_`{|}~-]";
	private static final String DOMAIN_EXPRESSION = ANY_CHAR_EXPRESSION + "+(\\." + ANY_CHAR_EXPRESSION + "+)*";
	private static final String IP_DOMAIN_REGEX = "^(2[5][0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})$";

	public static boolean isEmpty( Object obj ) {
		if ( obj == null ) {
			return true;
		} else if ( obj instanceof CharSequence && ( obj.equals ( "" ) ) ) {
			return true;
		} else if ( obj instanceof Number && ( (Number) obj ).doubleValue () == 0 ) {
			return true;
		} else if ( obj instanceof Boolean && !( (Boolean) obj ) ) {
			return true;
		} else if ( obj instanceof Collection && ( (Collection<?>) obj ).isEmpty () ) {
			return true;
		} else if ( obj instanceof Map && ( (Map<?, ?>) obj ).isEmpty () ) {
			return true;
		} else if ( obj instanceof Object[] && ( (Object[]) obj ).length == 0 ) {
			return true;
		}
		return false;
	}

	public static boolean notEmpty( Object obj ) {
		return !isEmpty ( obj );
	}

	public static boolean isMobile( CharSequence val ) {
		if ( isEmpty ( val ) ) {
			return false;
		}
		return Pattern.matches ( MOBILE_REGEX, val );
	}

	public static boolean isEmail( CharSequence val ) {
		Pattern pattern = java.util.regex.Pattern.compile ( "^" + ANY_CHAR_EXPRESSION + "+(\\." + ANY_CHAR_EXPRESSION + "+)*@(" + DOMAIN_EXPRESSION + ")$", Pattern.CASE_INSENSITIVE );
		if ( isEmpty ( val ) ) {
			return false;
		}
		Matcher matcher = pattern.matcher ( val );
		return matcher.matches ();
	}

	public static boolean length( int min, int max, CharSequence val ) {
		if ( min < 0 ) {
			throw new IllegalArgumentException ( "min不能小于0" );
		}
		if ( max < 0 ) {
			throw new IllegalArgumentException ( "max不能小于0" );
		}
		if ( max < min ) {
			throw new IllegalArgumentException ( "max不能小于min" );
		}
		if ( isEmpty ( val ) ) {
			return false;
		}
		int length = val.length ();
		return length >= min && length <= max;
	}

	public static boolean minSize( int min, CharSequence val ) {
		if ( min < 0 ) {
			throw new IllegalArgumentException ( "min不能小于0" );
		}
		return val.length () < min;
	}

	public static boolean maxSize( int max, CharSequence val ) {
		if ( max < 0 ) {
			throw new IllegalArgumentException ( "max不能小于0" );
		}
		return val.length () > max;
	}
	
	public static boolean isDomain( String address ) {
		if( isEmpty( address ) ) {
			return false;
		}
		String regex = "^" + DOMAIN_EXPRESSION + "$";
		return Pattern.matches(regex, address);
	}
	
	public static boolean isIp( String ip ) {
		if( isEmpty( ip ) ) {
			return false;
		}
		return Pattern.matches( IP_DOMAIN_REGEX , ip );
	}
	
	public static void main( String args [] ) {
		// String domain = "www.baidu.com";
		String domain = "www.baidu.com";
		
		System.out.println(ValidatorUtils.isDomain( domain ));
	}
}